package com.junzhiit.nengji.shop.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.shop.service.CartService;
import com.junzhiit.nengji.shop.web.vo.CartAddVO;
import com.junzhiit.nengji.shop.web.vo.CartEditVO;
import com.junzhiit.nengji.shop.web.vo.ResultVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/cart")
public class CartController extends BaseController {

	@Autowired
	private CartService cartService;

	@ApiOperation(value = "查询购物车商品信息(非堂食)")
	@RequestMapping(method = RequestMethod.GET)
	public ResultVO findAll(String userId,String shopId) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(shopId, "shopId is required.");
		return cartService.findAll(userId,shopId);
	}
	
	@ApiOperation(value = "查询购物车商品信息(堂食)")
	@RequestMapping(value="/eat", method = RequestMethod.GET)
	public ResultVO findEatAll(String userId,String shopId,Integer type) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(shopId, "shopId is required.");
		return cartService.findEatAll(userId,shopId,type);
	}

	@ApiOperation(value = "查询购物车商品sku")
	@RequestMapping(value = "/sku", method = RequestMethod.GET)
	public ResultVO findById(String goodsId) {
		AssertUtil.notBlank(goodsId, "goodsId is required.");
		return cartService.getGoodsSku(goodsId);
	}

	@ApiOperation(value = "加入购物车")
	@RequestMapping(method = RequestMethod.POST)
	public ResultVO add(CartAddVO cartAddVO) {
		AssertUtil.notBlank(cartAddVO.getUserId(), "userId is required.");
		AssertUtil.notBlank(cartAddVO.getShopId(), "shopId is required.");
		AssertUtil.notBlank(cartAddVO.getGoodsId(), "goodsId is required.");
		AssertUtil.notNull(cartAddVO.getNumber(), "number is required.");
		return cartService.add(cartAddVO);
	}

	@ApiOperation(value = "修改购物车信息")
	@RequestMapping(method = RequestMethod.PUT)
	public ResultVO update(CartEditVO cartEditVO) {
		AssertUtil.notBlank(cartEditVO.getId(), "id is required.");
		return cartService.edit(cartEditVO);
	}
	
	@ApiOperation(value = "批量删除购物车信息")
	@RequestMapping(value = "/batch/del", method = RequestMethod.PUT)
	public ResultVO delAddress(String[] ids) {
		AssertUtil.notNull(ids, "ids is required.");
		return cartService.batchDelCart(ids);
	}
	
	@ApiOperation(value = "查询购物车商品总数")
	@RequestMapping(value = "/sum",method = RequestMethod.GET)
	public ResultVO sumGoodsNumber(String userId,String shopId) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(shopId, "shopId is required.");
		return cartService.sumGoodsNumber(userId,shopId);
	}
}
